package com.platform.push.service.impl;


import com.platform.push.apiwx.ApiWX;
import com.platform.push.apiwx.ParamWXMiniMsg;
import com.platform.push.apiwx.WXResponse;
import com.platform.push.apiwx.mini.IApiWXMini;
import com.platform.base.utils.JsonUtil;
import com.platform.base.utils.Log;
import com.platform.push.service.PushWXMiniService;
import org.springframework.stereotype.Service;
import retrofit2.Response;

import javax.annotation.Resource;

/**
 * 微信小程序推送实现
 */
@Service
public class PushWXMiniImpl implements PushWXMiniService {
    @Resource
    private ApiWX mApiWX;
    public boolean doSend(String accessToken, String content) {
        try {
            IApiWXMini iApiWXMini = mApiWX.createApiWXMini();
            ParamWXMiniMsg msg = JsonUtil.fromJson(content, ParamWXMiniMsg.class);
            Response<WXResponse> response = iApiWXMini.sendSubscribeMsg(accessToken, msg).execute();
            if (!response.isSuccessful()) {
                Log.w(response.errorBody().string() + " - " + content);
                return false;
            }
            WXResponse wxResponse = response.body();
            if (wxResponse.getErrcode() != 0) {
                Log.w(JsonUtil.toJson(wxResponse) + " - " + content);
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(e);
            return false;
        }
    }
}
